package com.open.party.cloud.view.home.jiCengDangJian

import android.os.Bundle
import android.view.View
import cn.android.x.model.data.ResultInfo
import cn.sinothk.map.amap.base.MapCallback
import cn.sinothk.map.amap.location.AMapLocate
import cn.sinothk.map.amap.location.AMapLocationEntity
import com.alibaba.fastjson.JSON
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdate
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.CameraPosition
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.MultiPointItem
import com.open.party.cloud.R
import com.open.party.cloud.model.TownshipBean
import com.open.party.cloud.view.comm.MapViewBaseActivity
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.activity_jcdj_community_map.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class JcdjCommunityMapActivity : MapViewBaseActivity(), View.OnClickListener {

    var viewModel: BizViewModel? = null

    override fun getLayoutResId(): Int = R.layout.activity_jcdj_community_map

    override fun setMapMultiPointClickListener(): AMap.OnMultiPointClickListener {
        // 定义海量点点击事件
        return AMap.OnMultiPointClickListener { pointItem ->

            val infoStr: String? = pointItem.`object` as String
            if (XUtils.string().isNotEmpty(infoStr)) {
                val townshipBean: TownshipBean = JSON.parseObject(infoStr, TownshipBean::class.java)
                XUtils.intent().openActivity(this@JcdjCommunityMapActivity, OrgCountryDetailsActivity::class.java)
                    .putSerializableExtra("communityBean", townshipBean)
                    .start()
            }
            false
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("基层组织概况")

        viewModel = BizViewModel()
        initView()
    }

    private var mapLocEntity: AMapLocationEntity? = null

    private fun initView() {
        AMapLocate().locationOnce(this, object : MapCallback<AMapLocationEntity> {
            override fun onSuccess(locInfo: AMapLocationEntity) {
                mapLocEntity = locInfo
            }

            override fun onFail(msg: String?) {
            }
        })

        viewModel!!.getCommunityList("")

        delBtn.setOnClickListener {
            detailsView.visibility = View.GONE
        }

        searchBtn.setOnClickListener(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<ArrayList<TownshipBean>>) {
        if (result.eventType != "getCommunityList") {
            return
        }

        getMapData(result.data)
    }

    private fun getMapData(listData: ArrayList<TownshipBean>) {
        val mapList: ArrayList<MultiPointItem> = java.util.ArrayList()
        for (entity: TownshipBean in listData.iterator()) {
            if (entity.lat != null && entity.lng != null && entity.lat > 0 && entity.lng > 0) {
                val multiPoint = MultiPointItem(LatLng(entity.lat, entity.lng))
                multiPoint.title = XUtils.string().getNotNullValue(entity.name)
                multiPoint.snippet = XUtils.string().getNotNullValue(entity.address)
                multiPoint.`object` = JSON.toJSONString(entity)

                mapList.add(multiPoint)
            } else {
                continue
            }
        }
        if (mapList.size > 0) {
            setMultiPointOverlay(mapList)
            showMapView(mapList[0])
        } else {
            XUtils.toast().show("查无结果")
        }
    }

    private fun showMapView(pointItem: MultiPointItem) {
        //改变可视区域为指定位置
        //CameraPosition4个参数分别为位置，缩放级别，目标可视区域倾斜度，可视区域指向方向（正北逆时针算起，0-360）
        val cameraUpdate: CameraUpdate = CameraUpdateFactory.newCameraPosition(CameraPosition(pointItem.latLng, 14f, 0f, 30f))
        aMap.moveCamera(cameraUpdate) //地图移向指定区域
    }

    override fun onClick(v: View?) {
        when (v) {
            searchBtn -> {
                doMapSearch()
            }
        }
    }

    private fun doMapSearch() {

        val value = searchValue.text.toString()
        if (XUtils.string().isEmpty(value)) {
            XUtils.toast().show("请先输入地名")
            return
        }

        viewModel!!.getCommunityList(value)

//        //第二个参数传入null或者“”代表在全国进行检索，否则按照传入的city进行检索
//        val inputquery = InputtipsQuery(value, null)
//        inputquery.cityLimit = true //限制在当前城市
//
//        val inputTips = Inputtips(this@JcdjCommunityMapActivity, inputquery)
//        inputTips.setInputtipsListener { tipList, rCode ->
//            if (tipList != null && tipList.size > 0) {
//
//                val mapList: ArrayList<MultiPointItem> = java.util.ArrayList()
//                for (entity: Tip in tipList.iterator()) {
//
//                    if (entity.point != null && entity.point.latitude > 0 && entity.point.longitude > 0) {
//
//                        val multiPoint = MultiPointItem(LatLng(entity.point.latitude, entity.point.longitude))
//                        multiPoint.title = XUtils.string().getNotNullValue(entity.name)
//                        multiPoint.snippet = XUtils.string().getNotNullValue(entity.address)
//                        multiPoint.`object` = JSON.toJSONString(entity)
//
//                        mapList.add(multiPoint)
//                    } else {
//                        continue
//                    }
//                }
//                if (mapList.size > 0) {
//                    setMultiPointOverlay(mapList)
//                }
//            } else {
//                XUtils.toast().show("查无结果")
//            }
//        }
//        inputTips.requestInputtipsAsyn()
    }
}